package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.MesProducePackageRule;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.MesProducePackageRuleDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-06-07 08:15:33
 */
public class MesProducePackageRuleConverts {

    private static Logger LOG = LoggerFactory.getLogger(MesProducePackageRuleConverts.class);

    public static List<MesProducePackageRuleDTO> convert(List<MesProducePackageRule> mesProducePackageRuleList) {
        if (CollectionUtils.isEmpty(mesProducePackageRuleList)) {
            return Collections.EMPTY_LIST;
        }

        List<MesProducePackageRuleDTO> mesProducePackageRuleListResult = Lists.newArrayList();
        for (MesProducePackageRule mesProducePackageRule : mesProducePackageRuleList) {
            mesProducePackageRuleListResult.add(convert(mesProducePackageRule));
        }
        return mesProducePackageRuleListResult;
    }

    public static MesProducePackageRuleDTO convert(MesProducePackageRule mesProducePackageRule) {
        try {
            return BeanUtils.copy(mesProducePackageRule, MesProducePackageRuleDTO.class);
        } catch (Exception e) {
            LOG.error("MesProducePackageRule到MesProducePackageRuleDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesProducePackageRule到MesProducePackageRuleDTO转换失败", e);
        }
    }

    public static MesProducePackageRule convert(MesProducePackageRuleDTO mesProducePackageRuleDTO) {
        try {
            return BeanUtils.copy(mesProducePackageRuleDTO, MesProducePackageRule.class);
        } catch (Exception e) {
            LOG.error("MesProducePackageRuleDTO到MesProducePackageRule转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesProducePackageRuleDTO到MesProducePackageRule转换失败", e);
        }
    }
}